function Look(){}

var look = new Look();

/**
 * 提示信息
 */
Look.prototype.showInfo = {
	showTimer : $.timer(function() {
		look.showInfo.hide();
	}),
	add : function(){
		if($(".info_panel").length==0){
			$("body").prepend('<div class="info_panel" id="infoPanel"><span></span></div>');
		}
	},
	hide : function(){
		this.showTimer.stop();
		this.add();
		$("#infoPanel").fadeOut();
	},
	right : function(text){
		this.add();
		this.showTimer.stop();
		this.showTimer.once(5000);
		$("#infoPanel").fadeIn().children("span").removeClass("errorInfo").html(text);
	},
	error : function(text){
		this.right(text);
		$("#infoPanel span").addClass("errorInfo");
	}
}

/**
 * 生成id
 * @return
 */
Look.prototype.generateGuid = function(){
    var guid = "";
    for (var i = 1; i <= 32; i++){
          var n = Math.floor(Math.random()*16.0).toString(16);
          guid += n;
    }
    return guid;
}


/**
 * 重新设置参数值
 * @param {Array} key 如：[["aa","11"],["bb","22"]]
 * @param {String} source
 * @return
 */
Look.prototype.setParam = function(key, source){
	var reg = /^[#]|[?]/,
		type = reg.exec(source),
		theNewStr = source.replace(eval("/(^[" + type + "])/"), "").replace(/&hash_random=[a-zA-Z0-9]+/g, "");
	function single(keyStr, sourceStr){
		var arr = sourceStr.split("&"),
			newArr = "";
		for (var i = 0; i < arr.length; i++){
			if (arr[i].split("=")[0] == keyStr[0]) {
				newArr += keyStr[0] + "=" + keyStr[1] + "&";
			}else{
				newArr += arr[i].split("=")[0] + "=" + arr[i].split("=")[1] + "&";
			}
		}
		return newArr.replace(/[&]$/, "");
	}
	for(var j = 0; j < key.length; j++){
		theNewStr = single(key[j], theNewStr);
	}
	return type + theNewStr.replace(/[&]$/, "") + "&hash_random=" + look.generateGuid();
}

/**
 * 获取指定的URL中的hash参数值 
 * @param {String} paramName URL参数
 * @return paramValue URL参数值
 */
Look.prototype.getHashParam = function(paramName) {
	var paramValue = "";
	var isFound = false;
	if (location.hash.indexOf("#") == 0 && location.hash.indexOf("=") > 1) {
		var arrSource = unescape(location.hash).substring(1,location.hash.length).split("&");
		i = 0;
		while (i < arrSource.length && !isFound) {
			if (arrSource[i].indexOf("=") > 0) {
				if (arrSource[i].split("=")[0].toLowerCase() == paramName.toLowerCase()) {
					paramValue = arrSource[i].split("=")[1];
					isFound = true;
				}
			}
			i++;
		}
	}
	return paramValue;
}

/**
 * 获取String中的参数值
 * @param key
 * @param source
 * @return
 */
Look.prototype.getStringParam = function(key,source){
	if (source.indexOf("#") != -1 || source.indexOf("?") != -1) {
		source = source.substr(1);
	}
	if(source){
		var arr = source.split("&");
		for (var i=0; i<arr.length; i++){
			if(arr[i].split("=")[0] == key){
				return arr[i].split("=")[1];
			}
		}
	}
}

/**
 * 返回字符串在规定字节长度内的值  
 * @param val
 * @param max
 * @return String
 */
Look.prototype.getByteVal = function(val,max) {   
	var returnValue='';
	var byteValLen=0;
	var val=val.split("");
	for (var i=0;i<val.length;i++){
		if(val[i].match(/[^\x00-\xff]/ig)!=null){
			byteValLen+=2;
		}else {
			byteValLen+=1;
		}
		if (byteValLen>max){
			break;
		}
		returnValue+=val[i];
	}
	return returnValue;
}

/**
 * 数据请求方法
 * @param requestData
 * @param httpHeader
 * @param responseFn
 * @return
 */
Look.prototype.dataConn = function(url, requestData, responseFn, completeFn){
	$.ajax({
		type : "POST",
		url : url,
		dataType : "text",
		data : requestData,
		timeout : 15000,
		success : function(data, textStatus, XMLHttpRequest){
			var back = eval('(' + (data ? data : null) + ')');
			if(back != null){
				if(back.code != 0){
					look.showInfo.error(back.code + "：" + back.message);
					return false;
				}
				if(responseFn != undefined){
					try {
						eval(responseFn(back, textStatus));
					} catch (e) {
						look.showInfo.error("[ERROR] name: " + e.name + "message: " + e.message);
					}
				}
			}else{
				look.showInfo.error("网络连接出错！");
			}
		},
		error : function(XMLHttpRequest, error){
			if(error == "timeout"){
				look.showInfo.error("请求超时：请求系统返回数据超时！请稍候再试吧…");
			}else{
				look.showInfo.error(error + "：返回数据可能为非JSON格式…");
			}
		},
		complete : function(XMLHttpRequest, textStatus){
			if(completeFn != undefined){
				try {
					eval(completeFn(textStatus));
				} catch (e) {
					look.showInfo.error("[ERROR] name: " + e.name + "message: " + e.message);
				}
			}
		}
	});
}

/**
 * 同步加载页面
 * @param {String} container 页面存放的div容器
 * @param {String} url 加载的页面URL
 * @param {String} callback 回调函数
 */
Look.prototype.loadPage = function(container,url,callback) {
	$.ajax({
		url : url,
		async : false,
		cache: false,
		success : function(html) {
			$(container).html(html);
		},
		complete : function(XMLHttpRequest, textStatus) {
			if (callback != undefined) {
				try {
						eval(callback(textStatus));
				} catch (e) {
					look.showInfo.error("[ERROR] name: " + e.name + "message: " + e.message);
				}
			}
		},
		error : function(XmlHttpRequest, textStatus, errorThrown) {
			look.showInfo.error("[ERROR] 页面数据加载失败…");
		}
	});
}